/*
 * Copyright (C) 2011 Nanohex Corp.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.nanohexcorp.kudocode.snapqr.logic;

import java.util.ArrayList;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.nanohexcorp.kudocode.snapqr.data.QRLinksData;
import com.nanohexcorp.kudocode.snapqr.object.QRLinksObject;

/**
 * This class abstracts the data access layer from the business logic.
 *
 */
public class QRLinksLogic extends QRLinksData {
  private int indexRowID;
  private int indexTitle;
  private int indexUrl;
  private int indexDescription;

  // Constructor so simple save the database reference
  public QRLinksLogic(SQLiteDatabase db) {
    super(db);
  }

  // add new record using the object
  public void add(QRLinksObject qrLinksObject)
  {
	  this.insert(
	    qrLinksObject.getTitle(),
	    qrLinksObject.getUrl(),
	    qrLinksObject.getDescription()
	  );
  }

  // add new record using the parameters
  public void add(String title, String url, String description)
  {
	  this.insert(
	    title,
	    url,
	    description
	  );
  }

  //save/update record from an object
  public void remove(QRLinksObject qrLinksObject)
  {
	  this.remove(
		qrLinksObject.getID()
	  );
  }

  // save/update record using parameters
  public void remove(int id)
  {
	  this.delete(id);
  }

  // save/update record using parameters
  public void removeAll()
  {
	  this.deleteAll();
  }

  // save/update record from an object
  public void save(QRLinksObject qrLinksObject)
  {
	  this.update(
		qrLinksObject.getID(),
		qrLinksObject.getTitle(),
		qrLinksObject.getUrl(),
		qrLinksObject.getDescription()
	  );
  }

  // save/update record using parameters
  public void save(int id, String title, String url, String description)
  {
	  this.update(
	    id,
	    title,
	    url,
	    description
	  );
  }

  // get a record for an id
  public QRLinksObject get(int id)
  {
	  QRLinksObject qrLinksObject = null;
	  Cursor cursorQRLinks = this.selectByID(id);
	  prepCursorIndex(cursorQRLinks);
	  while(!cursorQRLinks.isAfterLast()) {
		  qrLinksObject = new QRLinksObject(
			cursorQRLinks.getInt(indexRowID),
			cursorQRLinks.getString(indexTitle),
			cursorQRLinks.getString(indexUrl),
			cursorQRLinks.getString(indexDescription)
		   );
		  cursorQRLinks.moveToNext();
	  }
	  cursorQRLinks.close();
	  return qrLinksObject;
  }

  // get all records into an ArrayList
  public ArrayList<QRLinksObject> getAll()
  {
	  ArrayList<QRLinksObject> arrayList = new ArrayList<QRLinksObject>();
	  Cursor cursorQRLinks = this.selectAll();
	  prepCursorIndex(cursorQRLinks);
	  while(!cursorQRLinks.isAfterLast()) {
		  QRLinksObject qrLinksObject = new QRLinksObject(
			cursorQRLinks.getInt(indexRowID),
			cursorQRLinks.getString(indexTitle),
			cursorQRLinks.getString(indexUrl),
			cursorQRLinks.getString(indexDescription)
		   );
		  arrayList.add(qrLinksObject);
		  cursorQRLinks.moveToNext();
	  }
	  cursorQRLinks.close();
	  return arrayList;
  }

  // used to get the indexes from the query
  private void prepCursorIndex(Cursor cursorQRLinks)
  {
	  indexRowID = cursorQRLinks.getColumnIndex(KEY_ROWID);
	  indexTitle = cursorQRLinks.getColumnIndex(KEY_TITLE);
	  indexUrl   = cursorQRLinks.getColumnIndex(KEY_URL);
	  indexDescription = cursorQRLinks.getColumnIndex(KEY_DESCRIPTION);
  }
}
